# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=mesa
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=23.1.6
pkgrel=2
pkgdesc="Open-source implementation of the OpenGL, Vulkan and OpenCL specifications (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-directx-headers"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-polly"
             "${MINGW_PACKAGE_PREFIX}-python-mako"
             "${MINGW_PACKAGE_PREFIX}-libclc"
             "${MINGW_PACKAGE_PREFIX}-libelf"
             "${MINGW_PACKAGE_PREFIX}-glslang"
             "git")
depends=("${MINGW_PACKAGE_PREFIX}-clang"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-vulkan-loader"
         "${MINGW_PACKAGE_PREFIX}-libsystre"
         "${MINGW_PACKAGE_PREFIX}-libva"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-zstd"
         "${MINGW_PACKAGE_PREFIX}-spirv-llvm-translator"
         "${MINGW_PACKAGE_PREFIX}-spirv-tools"
         "${MINGW_PACKAGE_PREFIX}-opencl-icd")
optdepends=("${MINGW_PACKAGE_PREFIX}-opengl-man-pages: for the OpenGL API man pages"
            "${MINGW_PACKAGE_PREFIX}-vulkan-validation-layers: to debug applications using Vulkan graphics")
url="https://www.mesa3d.org/"
license=('spdx:MIT')
source=(https://mesa.freedesktop.org/archive/${_realname}-${pkgver}.tar.xz{,.sig}
        llvmwrapgen.sh
        do-not-install-standard-headers.patch
        0001-Add-checks-for-NULL-dxil_validator.patch
        0002-meson-vaon12-fix-driver-file-name-for-mingw-build.patch
        0003-addrlib-workaround-old-cpu-target.patch
        0004-radv-add-stdcall-attribute.patch)
sha256sums=('f4c7fd8e7b472a88da7d83e9a48f6f3bd17d4ea2cc4386f7231b796f3964157a'
            'SKIP'
            '69f21522f20c10f5699dfe3e128aa88d4fedde816f6e8df1506d7470c44bf3da'
            'ac10033dd72e6ab705a8bdd5e2a47542a557d0cfc4c23fd0f319f682ec9308b8'
            '439bb27fc8201a6854f52550af7cffca1cecab05a5a89e335ac15f1dfce08832'
            '89b2dcef98cd5e9c5f39f79459cae6fd568577ebb380dffcc0c4e70cf8f9a82f'
            '4e511b1ab380d0e7d1f152f89c82665e5ef7de96f26de1b5597aa1db85cfc5a4'
            '77c31e7b63f6a01574be65d9a54f40e05cccd03eadae8ea3602ef5d17c4237a7')
validpgpkeys=('8703B6700E7EE06D7A39B8D6EDAE37B02CEB490D') # Emil Velikov <emil.l.velikov@gmail.com>
validpgpkeys+=('946D09B5E4C9845E63075FF1D961C596A7203456') # Andres Gomez <tanty@igalia.com>
validpgpkeys+=('E3E8F480C52ADD73B278EE78E1ECBE07D7D70895') # Juan Antonio Suárez Romero (Igalia, S.L.) <jasuarez@igalia.com>"
validpgpkeys+=('A5CC9FEC93F2F837CB044912336909B6B25FADFA') # Juan A. Suarez Romero <jasuarez@igalia.com>
validpgpkeys+=('71C4B75620BC75708B4BDB254C95FAAB3EB073EC') # Dylan Baker <dylan@pnwbakers.com>
validpgpkeys+=('57551DE15B968F6341C248F68D8E31AFC32428A6') # Eric Engestrom <eric@engestrom.ch>
noextract=(${_realname}-${pkgver}.tar.xz)

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  rm -rf ${srcdir}/${_realname}-${pkgver}
  tar -xJf ${srcdir}/${_realname}-${pkgver}.tar.xz -C ${srcdir} || true

  cd ${srcdir}/${_realname}-${pkgver}

# Do not install standard API headers
  apply_patch_with_msg \
    do-not-install-standard-headers.patch \
    0001-Add-checks-for-NULL-dxil_validator.patch \
    0002-meson-vaon12-fix-driver-file-name-for-mingw-build.patch \
    0003-addrlib-workaround-old-cpu-target.patch \
    0004-radv-add-stdcall-attribute.patch

# Generate binary wrap for LLVM if llvm-config tool is busted
  if ! [ "$(${MINGW_PREFIX}/bin/llvm-config --has-rtti 2>&1)" = YES ] && ! [ "$(${MINGW_PREFIX}/bin/llvm-config --has-rtti 2>&1)" = NO ]; then
    nollvmconfig=1 llvm_static=false ${srcdir}/llvmwrapgen.sh
  fi
}

build() {
  local -a _meson_options=(
  --prefix=${MINGW_PREFIX}
  --buildtype=release
  --backend=ninja
  -Dshared-glapi=disabled
  -Degl=disabled
  -Dgles1=disabled
  -Dgles2=disabled
  -Dllvm=enabled
  -Dshared-llvm=enabled
  -Dosmesa=true
  -Dbuild-tests=false
  -Dgallium-drivers=swrast,zink,d3d12
  -Dgallium-va=enabled
  -Dvideo-codecs=h264dec,h264enc,h265dec,h265enc,vc1dec
  -Dmicrosoft-clc=enabled
  -Dstatic-libclc=all
  -Dcpp_rtti=true
  -Dmin-windows-version=7
  -Dgallium-opencl=icd
  -Dopencl-spirv=true
  -Dvulkan-drivers=swrast,amd,microsoft-experimental
  )

# Fallback to binary wrap if llvm-config tool is busted
  if ! [ "$(${MINGW_PREFIX}/bin/llvm-config --has-rtti 2>&1)" = YES ] && ! [ "$(${MINGW_PREFIX}/bin/llvm-config --has-rtti 2>&1)" = NO ]; then
    _meson_options+=(
      --force-fallback-for=llvm
    )
  fi

  if [[ "${CARCH}" == "aarch64" ]]; then
    _cc_march=armv8-a
  else
    _cc_march=core2
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  PROCESSOR_ARCHITECTURE="${CARCH}" \
  ${MINGW_PREFIX}/bin/meson setup \
  -Dc_args="-march=${_cc_march} -pipe" \
  -Dcpp_args="-march=${_cc_march} -pipe" \
  -Dc_link_args='-s' \
  -Dcpp_link_args='-s' \
  "${_meson_options[@]}" \
  "build-${MSYSTEM}" \
  "${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/meson compile -C "build-${MSYSTEM}"
}

package() {
  ${MINGW_PREFIX}/bin/meson install --skip-subprojects --destdir "${pkgdir}" \
  -C "build-${MSYSTEM}"

# fix osmesa version in .pc file
  sed -e "s|8.0.0|${pkgver}|g" \
    -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/osmesa.pc

# Fix JSON for RADV
  if compgen -G "${pkgdir}${MINGW_PREFIX}/share/vulkan/icd.d/radeon_icd*.json" > /dev/null; then
    sed -e "s|./vulkan_radeon|${MINGW_PREFIX}/bin/libvulkan_radeon|g" \
      -i $(find ${pkgdir}${MINGW_PREFIX}/share/vulkan/icd.d -name "radeon_icd*.json" -print)
  fi
}
